package com.psy.counseling.utils;



import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * ClassName:TimeUtil
 * Package:com.psy.counseling.utils
 * Description:
 *
 * @Date:2022/6/1 12:04
 * @Author:cxh
 */
public class TimeUtil {
    /**
     * 将字符串转换为时间类
     * @param time
     * @return
     */
    public static Date strToDateTime(String time){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date newTime = null;
        try {
            newTime = format.parse(time);
        } catch ( ParseException e) {
            e.printStackTrace();
        }
        return newTime;
    }

    /**
     * 将时间转换为对应格式字符串
     * @param time
     * @return
     */
    public static String timeToStr(Date time){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strTime = format.format(time);
        return strTime;
    }

    /**
     * 时间运算操作
     * @param time
     * @param minute
     * @param add add表示默认
     * @return
     */
    public static String timeOperation(String time, Long minute,Boolean add){
        long millisecond = strToDateTime(time).getTime();
        if(add){
            millisecond += minute*60*1000;
        }else {
            millisecond -= minute*60*1000;
        }
        Date date = new Date(millisecond);
        String strTime = timeToStr(date);
        return strTime;
    }

    public static String timeOperation(String time, Long minute){
        Boolean add = true;
        return timeOperation(time,minute,add);
    }

    /**
     * 获取周几
     * @param date
     * @return
     */
    public static Integer getDayWeekOfDate(Date date){
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(date);
        int weekDay = aCalendar.get(Calendar.DAY_OF_WEEK);
        if((weekDay+6)%7!=0){
            weekDay = (weekDay+6)%7;
        }else{
            weekDay += 6;
        }
        return weekDay;
    }

    public static Integer getDayWeekOfDate(String time){
        Date date = strToDateTime(time);
        return getDayWeekOfDate(date);
    }

    /**
     * 获取时分秒
     * @param date
     * @return
     * @throws ParseException
     */
    public static String getTime(String date){
        String s = DateFormat.getTimeInstance().format(strToDateTime(date));
        return s;
    }

    /**
     * 将时间转换为yyyy-MM-dd 00:00:00
     * @param time
     * @return
     */
    public static String formatHhMmSsOfDate(String time,Boolean isStart) {
        if(isStart){
            return formatStart(time);
        }
        return formatEnd(time);
    }

    public static String formatStart(String time){
        String newTime = time.substring(0, time.lastIndexOf(" ") + 1)+"00:00:00";
        return newTime;
    }

    public static String formatEnd(String time){
        String newTime = time.substring(0, time.lastIndexOf(" ") + 1)+"23:59:59";
        return newTime;
    }

    /**
     * 将毫秒转换为hour
     * @param time
     * @return
     */
    public static int getHours(long time){
        int hours = (int)time/(1000*60*60);
        return hours;
    }

}
